'''
Nicholas Swartzendruber

This is a test program to read data
from the Reddit JSON api.

Required simplejson
http://simplejson.googlecode.com/svn/tags/simplejson-1.9.1/docs/index.html
'''

import simplejson
import urllib2

reddit_url = 'www.reddit.com/r/gaming'

req = urllib2.Request("http://" + reddit_url + "/.json", None, {'user-agent':'Reddit-Reader'})
opener = urllib2.build_opener()
try:
  f = opener.open(req)
  data = simplejson.load(f)
  
  print "{0:15}".format('Subreddit'), "{0:33}".format('Title') , 'URL'
  print'------------------------------------------------------------------------------------'
  for story in data['data']['children']:
    title = story['data']['title'];
    if(len(title) > 30):
      title = title[:27]
      title += '...'
    print "{0:15}".format(story['data']['subreddit']), "{0:33}".format(title) , story['data']['url']
except urllib2.HTTPError:
  print "Subreddit not found"
    
  

